-
-
Notifications
You must be signed in to change notification settings - Fork 4k
perf: optimize Object.keys() checks and optional chaining #15865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR implements several performance optimizations across the Mongoose codebase:
- Introduces a new
utils.hasOwnKeys()helper that checks for object keys more efficiently thanObject.keys().length - Converts verbose property access chains to optional chaining syntax
- Caches repeated property lookups to avoid redundant access
- Optimizes
indexOf()calls by caching results - Converts
argumentsobject usage to rest parameters - Optimizes Set operations for duplicate checking
Reviewed changes
Copilot reviewed 35 out of 35 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| lib/utils.js | Adds hasOwnKeys() helper function and updates isEmptyObject() to use it |
| lib/types/subdocument.js | Replaces Object.keys(ret).length === 0 with hasOwnKeys() |
| lib/types/documentArray/methods/index.js | Converts property access chains to optional chaining |
| lib/types/documentArray/index.js | Converts property access chains to optional chaining |
| lib/types/array/methods/index.js | Replaces Object.keys() length checks with hasOwnKeys() |
| lib/types/array/index.js | Converts property access chains to optional chaining |
| lib/schemaType.js | Simplifies null/undefined check to != null |
| lib/schema/subdocument.js | Converts to optional chaining and uses hasOwnKeys() |
| lib/schema/string.js | Simplifies null/undefined checks to != null |
| lib/schema/number.js | Simplifies null/undefined checks to != null |
| lib/schema/mixed.js | Replaces Object.keys() check with hasOwnKeys() |
| lib/schema/map.js | Replaces Object.keys() check with hasOwnKeys() |
| lib/schema/array.js | Converts property access chain to optional chaining |
| lib/schema.js | Converts to optional chaining and uses hasOwnKeys() |
| lib/queryHelpers.js | Converts to optional chaining |
| lib/query.js | Caches repeated property access, converts to rest parameters, uses hasOwnKeys(), and optional chaining |
| lib/plugins/trackTransaction.js | Replaces Object.keys() checks with hasOwnKeys() |
| lib/mongoose.js | Converts to optional chaining and uses hasOwnKeys() |
| lib/model.js | Converts to optional chaining and uses hasOwnKeys() |
| lib/helpers/update/castArrayFilters.js | Adds utils import and uses hasOwnKeys() |
| lib/helpers/update/applyTimestampsToUpdate.js | Adds utils import and uses hasOwnKeys() |
| lib/helpers/schema/getIndexes.js | Simplifies null/undefined check to != null |
| lib/helpers/schema/applyWriteConcern.js | Adds utils import, uses optional chaining and hasOwnKeys() |
| lib/helpers/schema/applyReadConcern.js | Converts to optional chaining |
| lib/helpers/query/castUpdate.js | Uses hasOwnKeys() for empty object checks |
| lib/helpers/populate/getModelsMapForPopulate.js | Optimizes duplicate checking with Set, uses optional chaining and hasOwnKeys() |
| lib/helpers/document/compile.js | Converts to optional chaining and uses hasOwnKeys() |
| lib/helpers/document/applyVirtuals.js | Adds utils import and uses hasOwnKeys() |
| lib/helpers/document/applyTimestamps.js | Adds utils import and uses hasOwnKeys() |
| lib/drivers/node-mongodb-native/connection.js | Converts to optional chaining and uses hasOwnKeys() |
| lib/drivers/node-mongodb-native/collection.js | Converts to optional chaining |
| lib/document.js | Optimizes with Set for key deduplication, caches schema options, uses hasOwnKeys() |
| lib/cursor/changeStream.js | Converts to optional chaining |
| lib/connection.js | Caches indexOf() results and uses hasOwnKeys() |
| lib/cast.js | Converts to optional chaining and uses hasOwnKeys() |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 35 out of 35 changed files in this pull request and generated no new comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Summary
utils.hasOwnKeys()helper that checks for empty objects without allocating an array viaObject.keys()Object.keys(obj).lengthpattern withhasOwnKeys(), reading the first key only instead of creating an array for all keys.a && a.b && a.b.cto optional chaininga?.b?.cthis.$__schema.options,model.db.options)indexOf()calls by caching the result instead of calling twiceargumentsto rest parameters inQuery.prototype.slice()Setoperations indocument.overwrite()andgetModelsMapForPopulate()